﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataMappers;
using System.Data.SqlClient;

using ConcreteMappersAndObjects.DomainObjects;
using System.Data;

namespace ConcreteMappersAndObjects.Mappers
{
    public class CUnitsDataMapper : AbstractDataMapper<int, CUnit>
    {
        private IEntityMapper<CUSemesterKey, CUSemester> _semestersMapper;
        private IEntityMapper<SubscriptionKey, Subscription> _subscriptionsMapper;
        private IEntityMapper<CourseCUKey, CourseCU> _coursesMapper;
        private IEntityMapper<TeacherClassCUnitKey, TeacherClassCUnit> _tccusMapper;

        public void SetTCCUMapper(IEntityMapper<TeacherClassCUnitKey, TeacherClassCUnit> mapper)
        {
            _tccusMapper = mapper;
        }
        public void SetSemestersMapper(IEntityMapper<CUSemesterKey, CUSemester> mapper)
        {
            _semestersMapper = mapper;
        }

        public void SetStudentsSubscritpionMapper(IEntityMapper<SubscriptionKey, Subscription> mapper)
        {
            _subscriptionsMapper = mapper;
        }

        public void SetCoursesMapper(IEntityMapper<CourseCUKey, CourseCU> mapper)
        {
            _coursesMapper = mapper;
        }

        protected override int GetKeyFrom(SqlDataReader sdr)
        {
            return sdr.GetInt32(0);
        }

        protected override string DoGetAllStatement()
        {
            return "select ID, Acronym, Name from CUnit";
        }

        protected override string DoGetFindStatement()
        {
            return DoGetAllStatement() + " where ID = @ID";
        }

        protected override void BindFindStatement(SqlCommand cmd, int key)
        {
            cmd.Parameters.AddWithValue("@ID", key);
        }

        private class CUnitsFilter : IFilter
        {
            private int _cunitKey;
            public CUnitsFilter(int cunitKey) { _cunitKey = cunitKey; }
            #region IFilter Members

            public string  GetWhereClause()
            {
 	            return " where CUnitID = @CUnitID";
            }

            public void  BindValues(SqlCommand cmd)
            {
 	            cmd.Parameters.AddWithValue("@CUnitID", _cunitKey);
            }

            #endregion
        }

        protected override CUnit DoLoad(SqlDataReader sdr)
        {
            int cunitKey = sdr.GetInt32(0);
            //to substitute
            //if (cunitKey == 7)
            //{
            //    Console.WriteLine();
            //}
            LazySet<CUSemesterKey, CUSemester> semesters = new LazySet<CUSemesterKey, CUSemester>(_semestersMapper, new CUnitsFilter(cunitKey));
            LazySet<SubscriptionKey, Subscription> subscriptions 
                = new LazySet<SubscriptionKey,Subscription>(_subscriptionsMapper , new CUnitsFilter(cunitKey));
            //to substitute
            LazySet<CourseCUKey, CourseCU> courses = new LazySet<CourseCUKey, CourseCU>(_coursesMapper, new CUnitsFilter(cunitKey));
            LazySet<TeacherClassCUnitKey, TeacherClassCUnit> tccus = new LazySet<TeacherClassCUnitKey, TeacherClassCUnit>(_tccusMapper, new CUnitsFilter(cunitKey));
            CUnit ret = new CUnit(cunitKey, sdr.GetString(1), sdr.GetString(2), semesters, subscriptions, courses, tccus);

            return ret;
        }

        protected override string GetUpdateCommand()
        {
            throw new NotImplementedException();
        }

        protected override void DoBindUpdate(SqlCommand cmd, CUnit t)
        {
            throw new NotImplementedException();
        }

        protected override string GetInsertCommand()
        {
            return "CUnitInsert";
        }

        protected override void DoBindInsert(SqlCommand cmd, CUnit t)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Acronym", SqlDbType.NVarChar, 8).Value = t.Acronym;
            cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 25).Value = t.Name;
        }

        protected override string GetRemoveCommand()
        {
            return "delete from CUnit where ID = @ID";
        }

        protected override void DoBindRemove(SqlCommand cmd, CUnit t)
        {
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = t.ID;
        }

        protected override void FinishUpdateObject(CUnit t)
        {
            
        }

        protected override void FinishNewObject(CUnit t, object id)
        {
            t.ID = Convert.ToInt32(id);
        }

        protected override void FinishDeleteObject(CUnit t)
        {
           
        }
    }
}
